package com.report.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.report.component.ScanCodePayComponent;
import com.report.dto.request.*;
import com.report.dto.response.*;
import com.report.dto.response.zu.ReportItemRes;
import com.report.dto.response.zu.WeChatMiniReportRes;

import com.report.entity.Member;
import com.report.entity.PDFImage;
import com.report.entity.Search;
import com.report.entity.Tag;
import com.report.service.*;
import com.report.utils.ImageUtils;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.oss.OssConfigure;
import com.zmh.framework.sys.dto.request.common.PageResponseDTO;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


/**
 * @author summer
 * @version 1.0
 * @date 2024/7/26 19:31
 * 小程序
 */
@RestController
@RequestMapping(value = "/weChatMini")
@AllArgsConstructor
public class WeChatMiniController {

    private final IReportService reportService;

    /**
     * 推荐/热门榜单
     * @param reportReq
     * @return
     */
    @PostMapping(value = "/recommendLists")
    public Result<Page<WeChatMiniReportRes>>   recommendLists(@RequestBody WeChatMiniReportReq reportReq) {
        Page<WeChatMiniReportRes> lists = reportService.wechatReportList(reportReq);
        return Result.successResult(lists);
    }

    private final IReportFileService reportFileService;

    @PostMapping(value = "/specialList")
    public Result<Page<ReportFileRes>> specialList(@RequestBody ReportFileReq reportFileReq) {
        Page<ReportFileRes> page=reportFileService.wechatSpecialList(reportFileReq);
        return Result.successResult(page);
    }


    /**
     * 查询报告二级页面
     * @param id
     * @return
     */
    @GetMapping(value = "/findReportById/{id}")
    public Result<ReportRes> findReportById(@PathVariable Integer id)  {
        ReportRes reportRes= reportService.findById(id);
        return Result.successResult(reportRes);
    }

    @PostMapping(value = "/findPDFImage")
    public Result<Page<PDFImage>> findPDFImage(@RequestBody ReportFileReq fileReq) {

       return Result.successResult(reportService.findPDFImage(fileReq));
    }

    /**
     * 根据ID查询专题
     * @param id
     * @return
     */
    @GetMapping(value = "/findSpecialListById/{id}")
    public Result<WechatMiniReportFileRes> findSpecialListById(@PathVariable("id") Integer id) {
        WechatMiniReportFileRes reportFileRes= reportFileService.findSpecialListById(id);
        return Result.successResult(reportFileRes);
    }

    @PostMapping(value = "/imageList")
    public Result<Page<ImageInfoRes>>  imageList(@RequestBody FileReq fileReq) {
        Page<ImageInfoRes> page = reportService.imageLists(fileReq);
        return Result.successResult(page);
    }

    private final IBrowseService browserService;

    @PostMapping(value = "/saveBrowse")
    public Result<String> saveBrowse(@RequestBody BrowseReq browseReq){
        browserService.saveBrowse(browseReq);
        return Result.successResult();
    }

    /**
     * 浏览记录
     * @param id
     * @return
     */
    @PostMapping(value = "/findBrowseList")
    public Result<Page<WebMemberBrowseRes>> findBrowseList(@RequestBody WebMemberCollectReq webMemberCollectReq){

        return Result.successResult(browserService.findBrowseList(webMemberCollectReq));
    }

    private final ICollectService collectService;

    /**
     * 浏览记录
     * @param id
     * @return
     */
    @PostMapping(value = "/findCollectList")
    public Result<Page<WebMemberCollectRes>> findCollectList(@RequestBody WebMemberCollectReq webMemberCollectReq){

        return Result.successResult(collectService.findCollectList(webMemberCollectReq));
    }


    private final ITagService tagService;

    @GetMapping(value = "/tagList")
    public Result<List<TagRes>>  tagList(){
        List<TagRes>  tagList= tagService.allTagLists();
        return Result.successResult(tagList);
    }


    @PostMapping(value = "/saveRecommend")
    public Result<String> saveRecommend(@RequestBody RecommendReq recommendReq){
        reportService.saveRecommend(recommendReq);
        return Result.successResult();
    }


    private final DownloadReportService downloadReportService;

    @PostMapping(value = "/lists")
    public Result<Page<DownloadReportRes>> lists(@RequestBody DownloadReportReq pageResponseDTO){
        Page<DownloadReportRes> page= downloadReportService.downloadReportLists(pageResponseDTO);
        return Result.successResult(page);
    }

    private final SubscribeService subscribeService;

    @PostMapping(value = "/subscribeLists")
     public Result<List<SubscribeRes>>  subscribeLists(@RequestBody SubscribeReq subscribeReq){

        return Result.successResult(subscribeService.subscribeLists(subscribeReq));
     }

     @PostMapping(value = "/saveSubscribe")
     public Result<String> saveSubscribe(@RequestBody SubscribeReq subscribeReq){
        subscribeService.saveSubscribe(subscribeReq);
        return Result.successResult();
     }

     @GetMapping(value = "/deleteSubscribe/{id}")
     public Result<String> deleteSubscribe(@PathVariable Integer id){
        subscribeService.removeById(id);
        return Result.successResult();
     }

     private final ReportItemService reportItemService;

    @PostMapping(value = "/reportList")
    public Result<Page<ReportItemRes>> reportList(@RequestBody ReportItemReq reportReq) {

        return Result.successResult(reportItemService.reportList(reportReq));
    }

    private final ISearchService searchService;

    @PostMapping(value = "/saveSearch")
    public Result<String> saveSearch(@RequestBody Search search){

        searchService.saveSearch(search);
        return Result.successResult();
    }


    @GetMapping(value = "/findSearch/{memberId}")
    public Result<Map<String,Object>> findSearch(@PathVariable Integer memberId){
        return Result.successResult(searchService.findSearch(memberId));
    }

    private final WxMaService wxMaService;

    private final OssConfigure ossConfigure;

    private final IMemberService memberService;

    @PostMapping(value = "/sendPoster")
    public Result<String> sendPoster(@RequestBody ReportReq reportReq){
        try {
            WxMaCodeLineColor codeLineColor = new WxMaCodeLineColor("0", "0", "0");

            File qrcode = wxMaService.getQrcodeService().createWxaCode("/pages/report/pdf/pdf?id=" + reportReq.getId(), "", 30, false, codeLineColor, false);
            String s1 = ossConfigure.uploadFile(qrcode);
            String code =ossConfigure.getAccessUrl()+s1;
            Member member = memberService.getById(reportReq.getMemberId());
            ReportRes reportRes = reportService.sendPoster(reportReq.getId());
             InputStream stream=ImageUtils.sendPosterInput(reportRes.getImage(), code, reportRes.getName(),member.getAvatarUrl());

            return Result.successResult(ossConfigure.getAccessUrl()+ ossConfigure.uploadFile(stream));
        }catch (Exception e){
            throw new ResultException(e.getMessage());
        }

    }

    @GetMapping(value = "/memberCheckTag/{memberId}")
    public Result<List<SubscribeRes>>  memberCheckTag(@PathVariable Integer memberId){
        List<SubscribeRes> subscribeRes= subscribeService.memberCheckTag(memberId);
        return Result.successResult(subscribeRes);
    }
    //

    @GetMapping(value = "/clearSearch/{memberId}")
    public Result<String> clearSearch(@PathVariable Integer memberId){
        subscribeService.clearSearch(memberId);
        return Result.successResult();
    }


    @GetMapping(value = "/findTagIndustry/{industryId}")
    public Result<String> findTagIndustry(@PathVariable Integer industryId){
        Tag tag = tagService.getById(industryId);
        return Result.successResult(tag.getName());
    }


    private ScanCodePayComponent scanCodePayComponent;

    private final WxMaProperties wxMaProperties;

    @PostMapping(value = "/jsApiPay")
    public Result<WxPayMpOrderResult> jsApiPay(@RequestBody WechatReq req, HttpServletRequest request){
        req.setAppId(wxMaProperties.getAppid());
        WxPayMpOrderResult wxPayMpOrderResult= scanCodePayComponent.jsApiPay(req,request);
        return Result.successResult(wxPayMpOrderResult);
    }


    @GetMapping(value = "/getMemberById/{id}")
    public Result<MemberRes> getMemberById(@PathVariable Integer id){

        return Result.successResult(memberService.getMemberById(id));
    }

    @PostMapping(value = "/readRankingList")
    public Result<List<ReadRankingRes>>  readRankingList(@RequestBody ReadRankingReq readRankingReq){
        return Result.successResult(memberService.readRankingList(readRankingReq));
    }

    @PostMapping(value = "/readRanking")
    public Result<ReadRankingRes> readRanking(@RequestBody ReadRankingReq readRankingReq){

        return Result.successResult(memberService.readRanking(readRankingReq));
    }

    @PostMapping(value = "/saveCollect")
    public Result<String> saveCollect(@RequestBody WebMemberCollectReq webMemberCollectReq){
        memberService.saveCollect(webMemberCollectReq);
        return Result.successResult();
    }

    @PostMapping(value = "/saveReadRanking")
    public Result<String> saveReadRanking(@RequestBody ReadRankingReq readRankingReq){
        memberService.saveReadRanking(readRankingReq);
        return Result.successResult();
    }

    /**
     * 修改会员
     * @param memberReq
     * @return
     */
    @PostMapping(value = "/modifyMember")
    public  Result<MemberRes> modifyMember(@RequestBody MemberReq memberReq){

        return  Result.successResult(memberService.modifyMember(memberReq));
    }

    /**
     * 每天下载记录
     * @param memberId
     * @return
     */
    @GetMapping(value = "/downloadDay/{memberId}")
    public Result<String> downloadDay(@PathVariable Integer memberId){
        downloadReportService.downloadDay(memberId);
        return Result.successResult();
    }

    /**
     * 保存下载记录
      * @param downloadReportReq
     * @return
     */
     @PostMapping(value = "/saveDownloadData")
    public Result<String> saveDownloadData(@RequestBody DownloadReportReq downloadReportReq){
         downloadReportService.saveDownloadData(downloadReportReq);
        return Result.successResult();
     }
}
